<template>
    <div class="info">
        <!-- 个人信息 -->
        <van-nav-bar left-arrow @click-left="$router.back()" right-text="保存"  @click-right="onClickRight" :fixed="true" title="个人信息" class="i-title" z-index="99" />
        <!-- 头像 & 昵称 & 介绍 -->
        <van-cell-group>
            <van-cell @click="openupload" title="头像" is-link>
                <template #default>
                    <img style="width:30px;height:30px;border-radius:50%" :src="userObj.photo" alt="">
                </template>
            </van-cell>
            <van-cell title="昵称" @click="changename" is-link :value="userObj.name"/>
            <van-cell title="介绍" @click="changeIntro" is-link :value="userObj.intro" />
        </van-cell-group>
        <!-- 性别 & 生日 -->
        <van-cell-group>
            <van-cell title="性别" @click="changeSex" :value="userObj.gender===0 ? '男':'女'" />
            <van-cell title="生日" @click="changeBirth" :value="userObj.birthday" />
        </van-cell-group>

        <!-- 弹出层 -->
        <van-popup v-model="showname" position="bottom" :style="{ height: '7%' }">
          <van-field v-model="userObj.name" required />
        </van-popup>
        <!-- 介绍 -->
        <van-popup v-model="showintro" position="bottom" :style="{ height: '7%' }">
          <van-field v-model="userObj.intro" />
        </van-popup>
        <!-- 性别 -->
        <van-popup v-model="showgender" position="bottom" :style="{ height: '30%' }">
          <van-picker
            :columns="columns"
            @change="onChange"
          />
        </van-popup>
        <!-- 生日 -->
        <van-popup v-model="showbirth" position="bottom" :style="{ height: '30%' }">
          <van-datetime-picker
            v-model="currentDate"
            type="date"
            title="选择年月日"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onConfirm"
          />
        </van-popup>

        <!-- 头像弹出层 -->
        <upload ref="upload" @changephoto="changephoto"></upload>
    </div>
</template>

<script>
import { apiUserself, apiUserselfInfo, apiEditUser } from '@/api/user.js'
import upload from './com/upload'
import dayjs from 'dayjs'

export default {
  components: {
    upload
  },
  data () {
    return {
      userObj: {},
      showname: false,
      showintro: false,
      showgender: false,
      showbirth: false,
      columns: ['男', '女'],
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date()
    }
  },
  async created () {
    const res1 = await apiUserself()
    console.log('用户自己信息', res1)
    this.$set(this.userObj, 'photo', res1.data.photo)
    this.$set(this.userObj, 'name', res1.data.name)
    this.$set(this.userObj, 'intro', res1.data.intro)
    const res2 = await apiUserselfInfo()
    console.log('个人资料', res2)
    this.$set(this.userObj, 'birthday', res2.data.birthday)
    this.$set(this.userObj, 'gender', res2.data.gender)
  },
  methods: {
    changename () {
      this.showname = true
    },
    changeIntro () {
      this.showintro = true
    },
    changeSex () {
      this.showgender = true
    },
    onChange (picker, value) {
      if (value === '男') {
        this.userObj.gender = 0
      } else {
        this.userObj.gender = 1
      }
    },
    changeBirth () {
      this.showbirth = true
    },
    onConfirm (value) {
      const time = dayjs(value).format('YYYY-MM-DD')
      this.userObj.birthday = time
      this.showbirth = false
    },
    // 点击保存 事件
    async onClickRight () {
      try {
        await apiEditUser(this.userObj)
        this.$toast.success('修改成功')
        // console.log(res)
      } catch (err) {
        this.$toast.fail('修改失败')
      }
    },
    // 点击头像打开弹窗
    openupload () {
      this.$refs.upload.show = true
    },
    changephoto (photo) {
      this.userObj.photo = photo
    }
  }
}
</script>

<style lang="less" scoped>
.info {
    margin-top: 46px;
    .i-title {
        background-color: #3e9df8;
        /deep/ .van-nav-bar__title {
            color: #fff;
        }
        /deep/.van-icon {
            color: #fff;
        }
        /deep/ .van-nav-bar__text {
            color: #fff;
        }
    }
}
</style>
